import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
/**
 * FirstUI is the first page or the menu page for the game which contain a instruction for the first time gamer.
 * @author Kridpak Vorametpasuk 5510546638
 * @version May 09 2013
 */
public class FirstUI extends JFrame {

	public static String name;
	private JPanel panelTop, panelLeft, panelBot, panelCen;
	private JTextField playName;
	private JLabel gameName, textLevel;
	private JComboBox level;
	private JButton start, help, howTo;
	private String[] listLevel = { "Easy", "Very Easy", "Super Easy" };

	/**
	 * constructor which will reset the player name every time try reach this page
	 */
	public FirstUI() {
		name = "";
	}

	/**
	 * create Graphic interface and add some command to each components
	 */
	public void initComponent() {

		gameName = new JLabel("Think IT up!!!");
		gameName.setFont(new Font("Serif", Font.BOLD, 48));
		panelTop = new JPanel();
		panelTop.add(gameName);

		add(panelTop, BorderLayout.NORTH);

		playName = new JTextField();
		playName.setText("Please Type your name Here!");
		playName.addActionListener(new StartListener());
		panelLeft = new JPanel();
		GridLayout boxLeft = new GridLayout(4, 1, 0, 8);
		panelLeft.setLayout(boxLeft);
		panelLeft.add(playName);
		textLevel = new JLabel("Please select your game's level");
		textLevel.setFont(new Font("Serif", Font.ITALIC, 22));
		panelLeft.add(textLevel);
		level = new JComboBox(listLevel);
		panelLeft.add(level);

		add(panelLeft, BorderLayout.WEST);

		panelCen = new JPanel();
		help = new JButton("Help?");
		help.addActionListener(new HelpListener());
		panelCen.add(help);

		howTo = new JButton("How to play?");
		panelCen.add(howTo, BorderLayout.SOUTH);
		howTo.addActionListener(new HowToPlayListener());

		add(panelCen, BorderLayout.CENTER);

		panelBot = new JPanel();
		start = new JButton("Start GAME");
		panelBot.add(start);
		start.addActionListener(new StartListener());
		add(panelBot, BorderLayout.SOUTH);
	}

	/**
	 * run is the method that will call the program to create the fame and set up the frame.
	 */
	public void run() {
		FirstUI first = new FirstUI();
		first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		first.setTitle("Think it Up!!");
		first.setSize(480, 250);
		first.initComponent();
		first.setVisible(true);
	}

	/**
	 * helpListener is the class that contain helping instruction for the gamer
	 *
	 */
	class HelpListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JOptionPane
					.showMessageDialog(
							null,
							"1. Insert Your Name\n2. Select Game's Level\n3. Click Start",
							"TO START the game?",
							JOptionPane.INFORMATION_MESSAGE);
		}

	}

	/**
	 * 
	 * howtoplayListener that give command for the button to show up rule for the game and how to play the game.
	 *
	 */
	class HowToPlayListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			JOptionPane
					.showMessageDialog(
							null,
							"1. click only 3 buttons\n2. buttons that you had clicked will be sum up\n3. if all the buttons sum up to the answer on the left hand side,you will go to next question",
							"How to play?", JOptionPane.INFORMATION_MESSAGE);

		}

	}

	/**
	 * 
	 * startListener that will call up game and check player's name and check the game level
	 */
	class StartListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String text = playName.getText();
			if (text.equalsIgnoreCase("Please Type your name Here!")) {
				JOptionPane.showMessageDialog(null, "Please type your name!!",
						"UNCOMPLETE DETAIL!!!", JOptionPane.WARNING_MESSAGE);
			} else {
				name = text;
				String selc = level.getSelectedItem().toString();
				if (selc.equals("Easy"))
					GameFactory.setFactory(new EasyGameFactory());
				else if (selc.equals("Very Easy"))
					GameFactory.setFactory(new VeryEasyGameFactory());
				else
					GameFactory.setFactory(new SuperEasyGameFactory());
				
				GameFactory factory = GameFactory.getInstance();
				GameUI game = new GameUI(factory);
				game.run();
				dispose();
			}
		}
	}
}
